
//
//  OrderDetailViewController.m
//  DaJiang
//
//  Created by sally on 16/10/10.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CancelOrderViewController.h"
#import "CellManager.h"
#import "DateUtil.h"
#import "ExpertDetailTextCell.h"
#import "FeedbackView.h"
#import "OrderDetailViewController.h"
#import "NewsDetailViewController.h"
#import "OrderListViewController.h"
#import "OrderExpertCell.h"
#import "OrderFreeCell.h"
#import "OrderModel.h"
#import "PayModel.h"
#import "OrderPayCell.h"
#import "ChatManager.h"
#import "WXApiTool.h"
#import "UITextView+Placeholder.h"
#import <AlipaySDK/AlipaySDK.h>

//涉及正文本、高度、cell装配、cell点击
//三textview（Y）、三大文本（Y）、打分（Y）、支付（Y）、订单+时间、拒绝原因+时间、计时、见面、
#define TextHead @"头像"
#define TextGrade @"打分"
#define TextChat @"私信"
#define TextPayWay @"选择支付方式"
#define TextPay @"支付" //@""
#define TextFillInComment @"填写评价"
#define TextViewFillInCommentTag 13
#define TextMyComment @"我的评价"
#define TextTextView @"编辑框" //@""
#define TextLabel @"文本块"    //@""
#define TextAskQuestion @"向专家请教的问题"
#define TextViewAskQuestionTag 10
#define TextMyQuestion @"我请教的问题"
#define TextFillInMine @"关于自己 可备注时间地点爱好"
#define TextViewFillInMineTag 11
#define TextAboutMe @"关于我"
#define TextOrderNo @"订单编号"
#define TextOrderTime @"发起时间"
#define TextCancelReason @"取消原因"
#define TextCancelTime @"取消时间"
#define TextKeepTime @"计时"
#define TextMeet @"见面" //个数待定。待见面页，需通过type另做判断，而非tabledata
#define TextNoAppear @"用户没出现"

#define ArrHead @[ TextHead ]
#define ArrGrade @[ TextGrade ]
#define ArrChat @[ TextChat ]
#define ArrPayWay @[ TextPayWay, TextPay ]
#define ArrFillInComment @[ TextFillInComment, TextTextView ]
#define ArrMyComment @[ TextMyComment, TextLabel ]
#define ArrAskQuestion @[ TextAskQuestion, TextTextView ]
#define ArrMyQuestion @[ TextMyQuestion, TextLabel ]
#define ArrFillInMine @[ TextFillInMine, TextTextView ]
#define ArrAboutMe @[ TextAboutMe, TextLabel ]
#define ArrOrder @[ TextOrderNo, TextOrderTime ]
#define ArrCancel @[ TextCancelReason, TextCancelTime ]
#define ArrKeepTime @[ TextKeepTime ]
#define ArrMeet @[ TextMeet ]
#define ArrNoAppear @[ TextNoAppear ]

@interface OrderDetailViewController () <CWStarRateViewDelegate, UITextViewDelegate,IChatManagerDelegate,OrderPayCellDelegate,WXApiToolDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *headerView;
//普通用户headview
@property (weak, nonatomic) IBOutlet UIView *normalHeadView;
@property (weak, nonatomic) IBOutlet UILabel *proOneLabel;
@property (weak, nonatomic) IBOutlet UILabel *proTwoLabel;
@property (weak, nonatomic) IBOutlet UILabel *proThreeLabel;
@property (weak, nonatomic) IBOutlet UILabel *proFourLabel;
@property (weak, nonatomic) IBOutlet UILabel *proFiveLabel;
@property (weak, nonatomic) IBOutlet UIImageView *proOneImage;
@property (weak, nonatomic) IBOutlet UIImageView *proTwoImage;
@property (weak, nonatomic) IBOutlet UIImageView *proThreeImage;
@property (weak, nonatomic) IBOutlet UIImageView *proFourImage;
@property (weak, nonatomic) IBOutlet UIImageView *proFiveImage;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *proTwoLeftLyt;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *proFourRightLyt;
//专家headview
@property (weak, nonatomic) IBOutlet UIView *expertHeadView;
@property (weak, nonatomic) IBOutlet UILabel *proStatusLabel;
//footerview
@property (weak, nonatomic) IBOutlet UIView *footerView;
@property (weak, nonatomic) IBOutlet UIButton *footerBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *footerBtnCenterLyt;
@property (weak, nonatomic) IBOutlet UIButton *footerCancelBtn;
@property (weak, nonatomic) IBOutlet UILabel *footerLabel;
//底按钮
@property (weak, nonatomic) IBOutlet UIView *btnView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *btnViewHeightLyt;
@property (weak, nonatomic) IBOutlet UIButton *priceBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *priceBtnWidthLyt;
@property (weak, nonatomic) IBOutlet UIButton *sendBtn;

@property (strong, nonatomic) NSMutableArray *tableData;
@property (strong, nonatomic) NSMutableArray *freeArr;
@property (strong, nonatomic) UserData *userData;
@property (assign, nonatomic) NSInteger expertid;

@property (strong, nonatomic) UIImageView *navBarHairlineImageView;
@property (strong, nonatomic) UIButton *noAppearBtn;//用户是否出现按钮
@property (assign, nonatomic) NSInteger freeSelectedIndex;
@property (assign, nonatomic) NSInteger selectStarNum;
@property (assign, nonatomic) OrderPayMethod payMethod;//支付方式

//textview中内容
@property (strong, nonatomic) NSString *question; //向专家请教的问题
@property (strong, nonatomic) NSString *aboutMe;  //关于自己
@property (strong, nonatomic) NSString *comment;  //评论

@end

@implementation OrderDetailViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self setNotification];
  [self initControl];
  [self initData];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  //黑导航栏-普通用户
  if (self.orderDetailType == OrderDetailType_Normal) {
    self.navBarHairlineImageView.hidden = YES;
    [[CommonMethod sharedCommonMethod] setNavAndStatusBar:self.navigationController.navigationBar style:NavStyle_Black];
  }
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  //默认导航栏-普通用户
  if (self.orderDetailType == OrderDetailType_Normal) {
    self.navBarHairlineImageView.hidden = NO;
    [[CommonMethod sharedCommonMethod] setNavAndStatusBar:self.navigationController.navigationBar style:NavStyle_Normal];
  }
}

- (void)dealloc {
  [self unregisterNotifications];
  [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)setNotification {
  [self registerNotifications];
  [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticePaySuccess:) name:NoticePaySuccess object:nil];
}

#pragma mark 初始化控件
- (void)initControl {
  self.navBarHairlineImageView = [self findHairlineImageViewUnder:self.navigationController.navigationBar];
  
  //按钮默认隐藏
  self.footerBtn.hidden = YES;
  self.footerView.height = 0;
  
  //底按钮默认隐藏
  self.btnView.hidden = YES;
  self.btnViewHeightLyt.constant = 0;
  
  //tip: title分类、header分类、按钮分类、底按钮分类（通过类型、状态）
  if (self.orderDetailType == OrderDetailType_Normal) { //普通用户
    self.headerView.height = GOrderHeadViewHeight;
    self.normalHeadView.hidden = NO;
    self.expertHeadView.hidden = YES;
    self.proTwoLeftLyt.constant = (ScreenWidth/2-GLargeMargin)/2 -11;//11为进度点图片带来的差距
    self.proFourRightLyt.constant = (ScreenWidth/2-GLargeMargin)/2 -11;
    NSString *title = @"";
    NSInteger progress = -1; //进度
    
    switch (self.orderNormalStatus) {
      case OrderNormalStatus_FillIn: //填写-两textview、底按钮(提交)
        title = @"预约";
        progress = 1;
        [self configBtnView:@"提交" isPrice:NO];
        [self.sendBtn addTarget:self action:@selector(clickOnFillInBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_ToBeConfirm: //待专家确认-两大文本、订单+时间、按钮(提醒专家回应)
        title = @"待专家确认";
        progress = 2;
        [self setRightBarButton];
        [self configFooterView:@"提醒专家回应" isGray:NO isLabel:NO isTwoBtn:NO];
        [self.footerBtn addTarget:self action:@selector(clickOnRemindBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_Reminded: //已提醒专家确认-两大文本、订单+时间、按钮（已提醒-灰）
        title = @"待专家确认";
        progress = 2;
        [self setRightBarButton];
        [self configFooterView:@"已提醒" isGray:YES isLabel:NO isTwoBtn:NO];
        [self.footerBtn removeTarget:self action:@selector(clickOnRemindBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_Rejected: //被拒绝-两大文本、订单+时间
        title = @"被拒绝";
        progress = 0;
        
        break;
      case OrderNormalStatus_Cancelled: //已取消-两大文本、订单+时间、拒绝原因+时间
        title = @"已取消";
        progress = 0;
        
        break;
      case OrderNormalStatus_ToPay: //待付款-计时、两大文本、订单+时间、底按钮（付款）
        title = @"付款";
        progress = 3;
        [self configBtnView:@"付款" isPrice:NO];
        [self.sendBtn addTarget:self action:@selector(clickOnToPayBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_PayIng: //付款中-计时、支付、订单+时间、底按钮（确认付款）
        title = @"付款";
        progress = 3;
        [self configBtnView:@"确认付款" isPrice:YES];
        [self.sendBtn addTarget:self action:@selector(clickOnPayIngBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_ToMeet: //待见面-见面、按钮（与专家私信）+文本、底按钮（提交）
        title = @"待见面";
        progress = 4;
        [self configBtnView:@"提交" isPrice:NO];
        [self.sendBtn addTarget:self action:@selector(clickOnToMeetBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self configFooterView:@"与专家私信" isGray:NO isLabel:YES isTwoBtn:NO];
        [self.footerBtn addTarget:self action:@selector(clickOnChatBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_NoMeet: //未见面-两大文本、订单+时间、见面
        title = @"待见面";
        progress = 4;
        
        break;
      case OrderNormalStatus_Comment: //评价-打分、textview、两大文本、订单+时间、见面、底按钮（提交）
        title = @"评价";
        progress = 5;
        [self configBtnView:@"提交" isPrice:NO];
        [self.sendBtn addTarget:self action:@selector(clickOnCommentBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderNormalStatus_Finished: //已完成-打分、三大文本、订单+时间、见面
        title = @"已完成";
        progress = 5;
        
        break;
      default:
        break;
    }
    self.title = title;
    [self setProgressView:progress];
    
  } else { //专家
    self.title = @"预约详情";
    self.headerView.height = GHeadViewHeight;
    self.normalHeadView.hidden = YES;
    self.expertHeadView.hidden = NO;
    NSString *statusStr = @"";
    
    switch (self.orderExpertStatus) {
      case OrderExpertStatus_ToBeConfirm: //待确认(同上)-两大文本、订单+时间、按钮（取消+确认）
        statusStr = @"等待确认";
        [self configFooterView:@"确认" isGray:NO isLabel:NO isTwoBtn:YES];
        [self.footerBtn addTarget:self action:@selector(clickOnConfirmBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.footerCancelBtn addTarget:self action:@selector(clickOnCancelBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderExpertStatus_ToMeet: //待见面-两大文本、订单+时间、见面、用户没出现、底按钮（完成见面）
        statusStr = @"等待见面";
        [self configBtnView:@"完成见面" isPrice:NO];
        [self.sendBtn addTarget:self action:@selector(clickOnMetBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        break;
      case OrderExpertStatus_Cancelled: //已取消（同上）-两大文本、订单+时间、拒绝原因+时间
        statusStr = @"订单已取消";
        
        break;
      case OrderExpertStatus_Finished: //已完成（同上）-打分、三大文本、订单+时间、见面
        statusStr = @"订单已完成";
        
        break;
      default:
        break;
    }
    
    self.proStatusLabel.text = statusStr;
  }
}

#pragma mark 初始化数据
- (void)initData {
  if (self.expertDetail && self.expertDetail.pro_user_info[0]) {
    UserModel *userModel = self.expertDetail.pro_user_info[0];
    self.userData = userModel.user_data;
    self.expertid = userModel.uid;
  }
  
  //tip: 数据分类（通过类型、状态）
  //三textview（Y）、三大文本（Y）、打分（Y）、支付（Y）、订单+时间、拒绝原因+时间、计时、见面、
  self.tableData = [NSMutableArray array];
  self.freeArr = [NSMutableArray array];
  self.freeSelectedIndex = GFreeSelectedInit;
  self.selectStarNum = 5;
  self.payMethod = OrderPayMethod_WX;
  self.question = @"";
  self.aboutMe = @"";
  self.comment = @"";

  [self.tableData addObject:ArrHead];
  
  if (self.orderDetailType == OrderDetailType_Normal) { //普通用户
    switch (self.orderNormalStatus) {
      case OrderNormalStatus_FillIn: //填写-两textview、底按钮
        [self.tableData addObject:ArrAskQuestion];
        [self.tableData addObject:ArrFillInMine];
        break;
      case OrderNormalStatus_ToBeConfirm: //待专家确认-两大文本、订单+时间、按钮
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderNormalStatus_Reminded: //已提醒专家确认-两大文本、订单+时间、按钮
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderNormalStatus_Rejected: //被拒绝-两大文本、订单+时间
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderNormalStatus_Cancelled: //已取消-两大文本、订单+时间、拒绝原因+时间
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrCancel];
        break;
      case OrderNormalStatus_ToPay: //待付款-计时、两大文本、订单+时间、底按钮
        //[self.tableData addObject:ArrKeepTime];//计时暂时去掉
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderNormalStatus_PayIng: //付款中-计时、支付、订单+时间、底按钮
        //[self.tableData addObject:ArrKeepTime];//计时暂时去掉
        [self.tableData addObject:ArrPayWay];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderNormalStatus_ToMeet: //待见面-见面、按钮+文本、底按钮
        [self quaryFreeList];
        break;
      case OrderNormalStatus_NoMeet: //未见面-两大文本、订单+时间、见面
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrMeet];
        break;
      case OrderNormalStatus_Comment: //评价-打分、textview、两大文本、订单+时间、见面、底按钮
        [self.tableData addObject:ArrGrade];
        [self.tableData addObject:ArrFillInComment];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrMeet];
        break;
      case OrderNormalStatus_Finished: //已完成-打分、三大文本、订单+时间、见面
        [self.tableData addObject:ArrGrade];
        [self.tableData addObject:ArrMyComment];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrMeet];
        break;
      default:
        break;
    }
  } else { //专家
    switch (self.orderExpertStatus) {
      case OrderExpertStatus_ToBeConfirm: //待确认(同上)-两大文本、订单+时间、按钮
        [self.tableData addObject:ArrChat];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        break;
      case OrderExpertStatus_ToMeet: //待见面-两大文本、订单+时间、见面、（用户没出现、底按钮）
        [self.tableData addObject:ArrChat];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrMeet];
        [self.tableData addObject:ArrNoAppear];
        
        break;
      case OrderExpertStatus_Cancelled: //已取消（同上）-两大文本、订单+时间、拒绝原因+时间
        [self.tableData addObject:ArrChat];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrCancel];
        break;
      case OrderExpertStatus_Finished: //已完成（同上）-打分、三大文本、订单+时间、见面
        [self.tableData addObject:ArrChat];
        [self.tableData addObject:ArrGrade];
        [self.tableData addObject:ArrMyComment];
        [self.tableData addObject:ArrMyQuestion];
        [self.tableData addObject:ArrAboutMe];
        [self.tableData addObject:ArrOrder];
        [self.tableData addObject:ArrMeet];
        break;
      default:
        break;
    }
  }
}

- (IBAction)actionOnBack:(id)sender {
  [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return self.tableData.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return [[self.tableData objectAtIndex:section] count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  return 0.1; //section头部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
  return GNormalMargin; //section底部高度
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  //tip: 高度分类（通过tableData）
  
  NSString *text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  if ([text isEqualToString:TextHead] || [text isEqualToString:TextPay] || [text isEqualToString:TextMeet]) {
    //cell高
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
  } else if ([text isEqualToString:TextTextView]) {
    //编辑框高
    return GTextViewHeight - NavHeight;
  } else if ([text isEqualToString:TextLabel]) {
    //文本高
    return [ExpertDetailTextCell cellHeight];
  } else {
    return NavHeight;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  //tip: cell分类（通过tableData）
  //cell：普通cell(打分，计时)、专家cell、见面cell、支付cell、文本Cell
  
  UITableViewCell *cell;
  NSString *text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  
  if ([text isEqualToString:TextHead]) {
    //专家cell
    OrderExpertCell *expertCell = (OrderExpertCell *) [tableView dequeueReusableCellWithIdentifier:@"OrderExpertCell"];
    if (!expertCell) {
      expertCell = [[[NSBundle mainBundle] loadNibNamed:@"OrderExpertCell" owner:nil options:nil] lastObject];
    }
    expertCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //装配数据
    if (self.orderModel) {
      expertCell.cellOrderData = self.orderModel;
    } else {
      expertCell.cellData = self.topicModel;
      expertCell.cellUserData = self.userData;
    }
    
    return expertCell;
    
  } else if ([text isEqualToString:TextPay]) {
    //支付cell
    OrderPayCell *payCell = (OrderPayCell *) [tableView dequeueReusableCellWithIdentifier:@"OrderPayCell"];
    if (!payCell) {
      payCell = [[[NSBundle mainBundle] loadNibNamed:@"OrderPayCell" owner:nil options:nil] lastObject];
    }
    payCell.selectionStyle = UITableViewCellSelectionStyleNone;
    payCell.delegate = self;
    
    return payCell;

  } else if ([text isEqualToString:TextMeet]) {
    //见面cell
    OrderFreeCell *freeCell = (OrderFreeCell *) [tableView dequeueReusableCellWithIdentifier:@"OrderFreeCell"];
    if (!freeCell) {
      freeCell = [[[NSBundle mainBundle] loadNibNamed:@"OrderFreeCell" owner:nil options:nil] lastObject];
    }
    freeCell.selectionStyle = UITableViewCellSelectionStyleDefault;
    
    if (self.orderNormalStatus == OrderNormalStatus_ToMeet) {
      freeCell.orderFreeType = OrderFreeType_Select;
      if (indexPath.row == self.freeSelectedIndex) {
        [freeCell setSelectBtnSelected:YES];
      }else{
        [freeCell setSelectBtnSelected:NO];
      }
      
      //装配数据
      freeCell.cellData = [self.freeArr objectAtIndex:indexPath.row];
    } else {
      freeCell.orderFreeType = OrderFreeType_Show;
      //装配数据
      freeCell.cellOrderData = self.orderModel;
    }
    
    return freeCell;
    
  } else if ([text isEqualToString:TextLabel]) {
    //文本cell
    ExpertDetailTextCell *textCell = (ExpertDetailTextCell *) [tableView dequeueReusableCellWithIdentifier:@"ExpertDetailTextCell"];
    if (!textCell) {
      textCell = [[[NSBundle mainBundle] loadNibNamed:@"ExpertDetailTextCell" owner:nil options:nil] lastObject];
    }
    textCell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    //装配数据
    NSString *titleStr = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:0];
    if ([titleStr isEqualToString:TextMyComment]) {
      //TODO: 评论字段暂无数据，待验证
      textCell.cellData = self.orderModel.op_data.comment;
    } else if ([titleStr isEqualToString:TextMyQuestion]) {
      textCell.cellData = self.orderModel.op_data.consulting_problem;
    } else if ([titleStr isEqualToString:TextAboutMe]) {
      textCell.cellData = self.orderModel.op_data.self_intro;
    }
    
    return textCell;
    
  } else {
    //普通cell
    //title（打分）、默认、黑、小橙（计时）、编辑框
    
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
    cell.textLabel.text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([text isEqualToString:TextPayWay]) { //默认
      [CellManager configCellNormalText:cell];
    } else if ([text isEqualToString:TextOrderNo] || [text isEqualToString:TextOrderTime] || [text isEqualToString:TextCancelReason] || [text isEqualToString:TextCancelTime] || [text isEqualToString:TextNoAppear]) { //黑
      [CellManager configCellText:cell type:TextColorType_Black];
      
      if ([text isEqualToString:TextNoAppear]) {
        self.noAppearBtn = [CellManager configCellAppearBtn];
        [self.noAppearBtn addTarget:self action:@selector(clickOnAppear:) forControlEvents:UIControlEventTouchUpInside];
        [cell.contentView addSubview:self.noAppearBtn];
      }
      
      //装配数据
      if ([text isEqualToString:TextOrderNo]) {
        cell.textLabel.text = [NSString stringWithFormat:@"订单编号：%ld", (long) self.orderModel.orderid];
      } else if ([text isEqualToString:TextOrderTime]) {
        cell.textLabel.text = [NSString stringWithFormat:@"发起时间：%@", [DateUtil getFormatDate:self.orderModel.created_at]];
      } else if ([text isEqualToString:TextCancelReason]) {
        cell.textLabel.text = [NSString stringWithFormat:@"取消原因：%@", self.orderModel.op_data.cancelled_reason];
      } else if ([text isEqualToString:TextCancelTime]) {
        cell.textLabel.text = [NSString stringWithFormat:@"取消时间：%@", [DateUtil getFormatDate:self.orderModel.updated_at]];
      }
      
    } else if ([text isEqualToString:TextKeepTime]) { //小橙（计时）
      //需求已去掉，暂时隐藏
      cell.textLabel.font = GFontWithSize(GSmallFontSize);
      cell.textLabel.textColor = GOrangeColor;
      //时间需要计算；文本、图片挤掉高度问题优化
      cell.textLabel.text = @"请于23小时58分37秒之内完成付款，超时该预约将自动取消";
      cell.textLabel.numberOfLines = 2;
      cell.imageView.image = [UIImage imageNamed:@"order_keep_time_ic"];
    } else if ([text isEqualToString:TextTextView]) { //编辑框
      UITextView *textView = [CellManager configCellTextView:cell];
      textView.delegate = self;
      [cell.contentView addSubview:textView];
      
      NSString *titleStr = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:0];
      if ([titleStr isEqualToString:TextAskQuestion]) {
        textView.placeholder = @"详细的问题描述有助于专家有的放矢";
        textView.text = self.question;
        textView.tag = TextViewAskQuestionTag;
      } else if ([titleStr isEqualToString:TextFillInMine]) {
        textView.placeholder = @"您的信息只有专家可以看到，不会公开";
        textView.text = self.aboutMe;
        textView.tag = TextViewFillInMineTag;
      } else if ([titleStr isEqualToString:TextFillInComment]) {
        textView.placeholder = @"见面之后的感受与对专家的认识";
        textView.text = self.comment;
        textView.tag = TextViewFillInCommentTag;
      }
    } else { //title
      [CellManager configCellTitleText:cell];
      if ([text isEqualToString:TextGrade]) { //打分
        BOOL isSelect = self.orderNormalStatus == OrderNormalStatus_Comment ? YES : NO;
        CWStarRateView *starRateView = [CellManager configCellStarRateView:isSelect];
        starRateView.delegate = self;
        //服务端是10分制，预约详情显示时是5分制，做2倍运算
        starRateView.scorePercent = self.orderNormalStatus == OrderNormalStatus_Comment ? 0 : self.orderModel.op_data.cmt_value/10.0;
        [cell.contentView addSubview:starRateView];
      }else if ([text isEqualToString:TextChat]) { //私信
        cell.textLabel.text = [NSString stringWithFormat:@"%@预约",self.orderModel.user.user_data.user_name];
        UIButton *chatBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 80 , 12, 65, 20)];
        [chatBtn setTitle:@"私信" forState:UIControlStateNormal];
        [chatBtn setTitleColor:GOrangeColor forState:UIControlStateNormal];
        chatBtn.titleLabel.font = GFontWithSize(GMiddleFontSize);
        chatBtn.layer.cornerRadius = 20/2;
        chatBtn.layer.masksToBounds = YES;
        chatBtn.layer.borderColor = GOrangeColor.CGColor;
        chatBtn.layer.borderWidth = 1;
        chatBtn.userInteractionEnabled = NO;
        [cell.contentView addSubview:chatBtn];
      }
    }
  }
  
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  
  NSString *text = [[self.tableData objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
  if (self.orderNormalStatus == OrderNormalStatus_ToMeet && [text isEqualToString:TextMeet]) {
    self.freeSelectedIndex = indexPath.row;
    [self.tableView reloadData];
  }
  
  //“约我的人”订单都加私信入口
  if ([text isEqualToString:TextChat]) {
    //添加数据到数据库 从用户页新建会话时再插入数据库
    [self addExtDateToDB:YES];
    
    //新建环信会话
    NSString *chatter = [NSString stringWithFormat:@"%@%ld",HXPrefix,(long)self.orderModel.user.uid];
    [self gotoNewsDetail:chatter];
  }
}

#pragma mark - UITextViewDelegate
- (void)textViewDidChange:(UITextView *)textView {
  if (textView.tag == TextViewAskQuestionTag) {
    self.question = textView.text;
  } else if (textView.tag == TextViewFillInMineTag) {
    self.aboutMe = textView.text;
  } else if (textView.tag == TextViewFillInCommentTag) {
    self.comment = textView.text;
  }
}

#pragma mark CWStarRateViewDelegate 打分分数
- (void)getSelectStarNum:(CGFloat)num {
  self.selectStarNum = num;
}

#pragma mark 注册delegate
- (void)registerNotifications {
  [self unregisterNotifications];
  [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
}

- (void)unregisterNotifications {
  [[EaseMob sharedInstance].chatManager removeDelegate:self];
}

#pragma mark OrderPayCellDelegate 支付方式
- (void)selectPayMethod:(OrderPayMethod)method{
  self.payMethod = method;
}

#pragma mark WXApiToolDelegate 微信支付成功后回调
- (void)noticeWXPaySuccess{
  [self noticePaySuccess:nil];
}

#pragma mark - 私有方法
#pragma mark 设置导航栏右按钮
- (void)setRightBarButton {
  self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"取消预约" style:UIBarButtonItemStyleDone target:self action:@selector(cancelOrder)];
  [[CommonMethod sharedCommonMethod] setNavButtonItem:self.navigationItem.rightBarButtonItem type:ButtonItemType_White];
}

#pragma mark 取消预约
- (void)cancelOrder {
  CancelOrderViewController *vc = [[CancelOrderViewController alloc] init];
  vc.consultId = self.orderModel.orderid;
  [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark 配置按钮
- (void)configFooterView:(NSString *)title isGray:(BOOL)isGray isLabel:(BOOL)isLabel isTwoBtn:(BOOL)isTwoBtn {
  self.footerView.height = GOrderFootViewHeight;
  self.footerBtn.hidden = NO;
  [self.footerBtn setTitle:title forState:UIControlStateNormal];
  
  if (isGray) {
    self.footerBtn.layer.borderColor = GGrayWordColor2.CGColor;
    [self.footerBtn setTitleColor:GGrayWordColor2 forState:UIControlStateNormal];
  } else {
    self.footerBtn.layer.borderColor = GOrangeColor.CGColor;
    [self.footerBtn setTitleColor:GOrangeColor forState:UIControlStateNormal];
  }
  self.footerBtn.layer.borderWidth = 1;
  self.footerBtn.layer.cornerRadius = GNormalBtnHeight / 2;
  self.footerBtn.layer.masksToBounds = YES;
  
  //是否显示描述文本
  self.footerLabel.hidden = !isLabel;
  
  if (isTwoBtn) {//是否是两个按钮
    self.footerView.height = GOrderFootViewHeight + GSmallMargin + 15; //15文本高度
    self.footerCancelBtn.hidden = NO;
    self.footerCancelBtn.layer.borderColor = GGrayWordColor2.CGColor;
    self.footerCancelBtn.layer.borderWidth = 1;
    self.footerCancelBtn.layer.cornerRadius = GNormalBtnHeight / 2;
    self.footerCancelBtn.layer.masksToBounds = YES;
    //FIXME: 为什么要xib中取消勾选size class才能使footerBtnCenterLyt设置生效
    self.footerBtnCenterLyt.constant = 90;
  } else {
    self.footerCancelBtn.hidden = YES;
    self.footerBtnCenterLyt.constant = 0;
  }
}

#pragma mark 配置底按钮
- (void)configBtnView:(NSString *)title isPrice:(BOOL)isPrice {
  self.btnView.hidden = NO;
  self.btnViewHeightLyt.constant = GLongBtnHeight;
  
  if (isPrice) { //是否显示价格按钮
    self.priceBtn.hidden = NO;
    [self.priceBtn setTitle:[NSString stringWithFormat:@"¥%.2f",self.orderModel.op_data.pro_topic_price] forState:UIControlStateNormal];
    self.priceBtnWidthLyt.constant = 80;
  } else {
    self.priceBtn.hidden = YES;
    self.priceBtnWidthLyt.constant = 0;
  }
  [self.sendBtn setTitle:title forState:UIControlStateNormal];
}

#pragma mark 设置进度控件
- (void)setProgressView:(NSInteger)progress {
  if (progress >= 1) {
    self.proOneLabel.textColor = [UIColor whiteColor];
    self.proOneImage.image = [UIImage imageNamed:@"order_point_ok_ic"];
  }
  if (progress >= 2) {
    self.proTwoLabel.textColor = [UIColor whiteColor];
    self.proTwoImage.image = [UIImage imageNamed:@"order_point_ok_ic"];
  }
  if (progress >= 3) {
    self.proThreeLabel.textColor = [UIColor whiteColor];
    self.proThreeImage.image = [UIImage imageNamed:@"order_point_ok_ic"];
  }
  if (progress >= 4) {
    self.proFourLabel.textColor = [UIColor whiteColor];
    self.proFourImage.image = [UIImage imageNamed:@"order_point_ok_ic"];
  }
  if (progress >= 5) {
    self.proFiveLabel.textColor = [UIColor whiteColor];
    self.proFiveImage.image = [UIImage imageNamed:@"order_point_ok_ic"];
  }
}

#pragma mark 隐藏\显示导航栏下的黑线
- (UIImageView *)findHairlineImageViewUnder:(UIView *)view {
  if ([view isKindOfClass:UIImageView.class] && view.bounds.size.height <= 1.0) {
    return (UIImageView *) view;
  }
  for (UIView *subview in view.subviews) {
    UIImageView *imageView = [self findHairlineImageViewUnder:subview];
    if (imageView) {
      return imageView;
    }
  }
  return nil;
}

#pragma mark 获取专家有空列表
- (void)quaryFreeList {
  [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  [FreeModel queryExpertFreeListWithProUid:self.orderModel.op_data.pro_uid completion:^(NSMutableArray *freeList) {
    if (freeList) {
      self.freeArr = freeList;
      
      NSMutableArray *meetArr = [[NSMutableArray alloc] init];
      for (int i = 0; i < freeList.count; i++) {
        [meetArr addObject:TextMeet];
      }
      [self.tableData addObject:meetArr];
      [self.tableView reloadData];
    } else {
      NSError *error = [FreeModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  }];
}

#pragma mark 提醒服务端支付成功
- (void)noticePaySuccess:(NSNotification *)notification{
  
  if (notification != nil) {//支付宝
    NSDictionary *userInfo = notification.userInfo;
    NSString *resultJsonStr = [userInfo valueForKey:@"result"];
    NSDictionary *result = [[CommonMethod sharedCommonMethod]dictionaryWithJsonString:resultJsonStr];
    NSString *orderNo = [[result valueForKey:@"alipay_trade_app_pay_response"]valueForKey:@"out_trade_no"];
    if (orderNo.integerValue == self.orderModel.orderid) {
      [self paySuccessVcOperation];
    }
    
  }else{//微信
    MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
    
    //通知服务端状态
    [OrderModel surePayedWithConsultId:self.orderModel.orderid proUid:self.orderModel.op_data.pro_uid cashValue:self.orderModel.op_data.pro_topic_price topicName:self.orderModel.op_data.pro_topic_name completion:^(BOOL isSucceed) {
      if (isSucceed) {
        [self paySuccessVcOperation];
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
    }];
  }

}

#pragma mark 支付成功页面操作
- (void)paySuccessVcOperation{
  UIWindow *window = [UIApplication sharedApplication].keyWindow;
  [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已确认付款"];
  
  //退到下下级列表页面
  for (UIViewController *vc in self.navigationController.viewControllers) {
    if ([vc isKindOfClass:[OrderListViewController class]]) {
      [self.navigationController popToViewController:vc animated:YES];
      
      //刷新订单列表数据
      [[NSNotificationCenter defaultCenter] postNotificationName:NoticeRefreshOrderListVc object:nil userInfo:nil];
    }
  }
}

#pragma mark - 数据库操作 添加专家信息到扩展字段数据库表
-(void)addExtDateToDB:(BOOL)isPro{
  Ext *ext = [[Ext alloc]init];

  //isPro 是否是专家端
  if (isPro) {//“约我的人”订单的私信
    ext.uid = [NSString stringWithFormat:@"%ld", (long)self.orderModel.user.uid];
    ext.avatar = self.orderModel.user.user_data.user_profile_pic;
    ext.nickname = self.orderModel.user.user_data.user_name;
    ext.username = [NSString stringWithFormat:@"%@%ld",HXPrefix, (long)self.orderModel.user.uid];
  }else{
    ext.uid = [NSString stringWithFormat:@"%ld", (long)self.orderModel.op_data.pro_uid];
    ext.avatar = self.orderModel.op_data.pro_user_profile_pic;
    ext.nickname = self.orderModel.op_data.pro_user_name;
    ext.username = [NSString stringWithFormat:@"%@%ld",HXPrefix, (long)self.orderModel.op_data.pro_uid];
  }
  
  [ChatManager addExtDateToDB:ext];
}

#pragma mark 新建环信会话
- (void)gotoNewsDetail:(NSString *)chatter {
  EMConversation *conversation = [[EaseMob sharedInstance].chatManager conversationForChatter:chatter conversationType:eConversationTypeChat];
  NewsDetailViewController *newDetailVC = [[NewsDetailViewController alloc] init];
  newDetailVC.conversation = conversation;
  newDetailVC.hidesBottomBarWhenPushed = YES;
  [self.navigationController pushViewController:newDetailVC animated:YES];
}

#pragma mark - 点击动作
#pragma mark 点击填写的“提交”
- (void)clickOnFillInBtn:(UIButton *)btn {
  if (self.topicModel) {
    if (self.question.length < 1) {
      [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入请教的问题"];
      return;
    }
    if (self.aboutMe.length < 1) {
      [[CommonMethod sharedCommonMethod]showToastInCenter:self.view withMessage:@"请填入关于自己"];
      return;
    }

    MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
    
    //预约专家的一个话题
    [OrderModel bookOrderWithProTopicId:self.topicModel.topicid proTopicName:self.topicModel.data.topic_name proTopicPrice:self.topicModel.data.topic_price proUid:self.expertid problem:self.question intro:self.aboutMe proPic:self.userData.user_profile_pic proName:self.userData.user_name proTitle:self.userData.user_pro_title completion:^(BOOL isSucceed) {
      if (isSucceed) {
        FeedbackView *feedbackView = [[FeedbackView alloc] initWithFeedbackType:FeedbackType_OrderWrite];
        [feedbackView show];
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
    }];
  }
}

#pragma mark 点击待专家确认的“提醒”
- (void)clickOnRemindBtn:(UIButton *)btn {
  [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  // 去掉了客户端发消息的需求，直接服务端发
  //  //添加数据到数据库 从用户页新建会话时插入数据库
  //  [self addExtDateToDB];
  //
  //  //ext:发送者的昵称和头像URL
  //  UserModel *userModel = [[CacheManager shareInstance] getLastLoginAccount];
  //  NSMutableDictionary *extDic = [[NSMutableDictionary alloc]init];
  //  [extDic setJsonValue:userModel.user_data.user_profile_pic key:@"avatar"];
  //  extDic[@"uid"] = @(userModel.uid);
  //  [extDic setJsonValue:userModel.user_data.user_name key:@"nickname"];
  //
  //  //发条环信消息给专家，发送完消息回调中调用接口，用以通知服务端状态、
  //  NSString *chatter = [NSString stringWithFormat:@"%@%ld",HXPrefix,(long)self.orderModel.op_data.pro_uid];
  //  [ChatManager sendTextMessage:@"提醒您关注一下我的咨询，谢谢。" to:chatter messageType:eMessageTypeChat requireEncryption:NO messageExt:extDic];
  
  //提醒专家，通知服务端状态更新
  [OrderModel remindExpertWithProUid:self.orderModel.op_data.pro_uid consultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
    if (isSucceed) {
      FeedbackView *feedbackView = [[FeedbackView alloc] initWithFeedbackType:FeedbackType_OrderRemind];
      [feedbackView show];
      //转成已提醒状态
      [self configFooterView:@"已提醒" isGray:YES isLabel:NO isTwoBtn:NO];
      [self.footerBtn removeTarget:self action:@selector(clickOnRemindBtn:) forControlEvents:UIControlEventTouchUpInside];
    } else {
      NSError *error = [OrderModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
  }];
}

#pragma mark 点击待付款的“付款”
- (void)clickOnToPayBtn:(UIButton *)btn {
  //跳转付款中状态的预约页
  OrderDetailViewController *vc = [OperationManager gotoOrderDetailVcWithType:OrderDetailType_Normal status:OrderNormalStatus_PayIng];
  vc.orderModel = self.orderModel;
}

#pragma mark 点击付款中的“确认付款”
- (void)clickOnPayIngBtn:(UIButton *)btn {
  //先调用支付宝sdk或微信支付sdk；微信支付成功后调用支付接口，用以通知服务端状态；支付宝支付成功后，不需要通知服务端状态
  
  NSString *orderNum = [NSString stringWithFormat:@"%ld",(long)self.orderModel.orderid];
  NSString *orderName = self.orderModel.op_data.pro_topic_name;

  if (self.orderModel.op_data.pro_topic_price > 0) {
    
    if (self.payMethod == OrderPayMethod_WX) {//微信支付
      NSString *orderPrice = [NSString stringWithFormat:@"%ld",(long)(self.orderModel.op_data.pro_topic_price * 100)];
      [[WXApiTool sharedWXApiTool] WXPayWithOrderName:orderName orderNum:orderNum price:orderPrice];
      [WXApiTool sharedWXApiTool].delegate = self;
      //TODO: 支付完成的回调的实现，通过onResp不太好
      
    }else{
      NSString *price = [NSString stringWithFormat:@"%.2f",self.orderModel.op_data.pro_topic_price];
      
      [PayModel payAlipayPrepayWithOrderId:orderNum subject:orderName totalFee:price completion:^(PayModel *payModel) {
        if (payModel) {
          
          // NOTE: 如果加签成功，则继续执行支付
          if (payModel.sign != nil) {
            //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
            NSString *appScheme = @"alisdkdajiang";
            
            // NOTE: 调用支付结果开始支付
            [[AlipaySDK defaultService]payOrder:payModel.order_info fromScheme:appScheme callback:^(NSDictionary *resultDic) {
              NSLog(@"reslut = %@",resultDic);
            }];
          }
          
        }else{
          NSError *error = [PayModel getLastError];
          [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
        }
      }];
      
    }
    
    
  }else{
    [[CommonMethod sharedCommonMethod]showAlertMessage:@"此订单无效"];
  }
  
}

#pragma mark 点击待见面的“提交”
- (void)clickOnToMeetBtn:(UIButton *)btn {
  if (self.freeSelectedIndex > GFreeSelectedInit) {
    MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
    
    //提交见面时间地点
    FreeModel *freeModel = [self.freeArr objectAtIndex:self.freeSelectedIndex];
    [OrderModel chooseFreeWithFreeId:freeModel.freeid consultId:self.orderModel.orderid meetTime:freeModel.data.time meetLoc:freeModel.data.location completion:^(BOOL isSucceed) {
      if (isSucceed) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已提交见面"];
        //退到下级列表页面
        [self.navigationController popViewControllerAnimated:YES];
        //刷新订单列表数据
        if (self.delegate && [self.delegate respondsToSelector:@selector(refreshOrderListVc)]) {
          [self.delegate refreshOrderListVc];
        }
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
    }];
    
  }else{
    [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请选择见面时间、地点"];
  }
}

#pragma mark 点击待见面的“与专家私信”
- (void)clickOnChatBtn:(UIButton *)btn {
  //添加数据到数据库 从用户页新建会话时再插入数据库
  [self addExtDateToDB:NO];
  
  //新建环信会话
  NSString *chatter = [NSString stringWithFormat:@"%@%ld",HXPrefix,(long)self.orderModel.op_data.pro_uid];
  [self gotoNewsDetail:chatter];
}

#pragma mark 点击评价的“提交”
- (void)clickOnCommentBtn:(UIButton *)btn {
  
  if (self.comment) {
    MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
    
    //评论
    //服务端是10分制，预约详情显示时是5分制，做2倍运算
    [OrderModel commentExpertWithComment:self.comment topicName:self.orderModel.op_data.pro_topic_name proUid:self.orderModel.op_data.pro_uid cmtValue:self.selectStarNum*2 topicId:self.orderModel.pro_topic.topicid consultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
      if (isSucceed) {
        FeedbackView *feedbackView = [[FeedbackView alloc] initWithFeedbackType:FeedbackType_OrderComment];
        [feedbackView show];
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
      
    }];
  }else{
    [[CommonMethod sharedCommonMethod] showToastInCenter:self.view withMessage:@"请输入评论"];
  }
}

#pragma mark 专家-点击待确认的“确认”
- (void)clickOnConfirmBtn:(UIButton *)btn {
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  [OrderModel expertConfirmOrderWithConsultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
    if (isSucceed) {
      UIWindow *window = [UIApplication sharedApplication].keyWindow;
      [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已确认预约"];
      //退到下级列表页面
      [self.navigationController popViewControllerAnimated:YES];
      //刷新订单列表数据
      if (self.delegate && [self.delegate respondsToSelector:@selector(refreshOrderListVc)]) {
        [self.delegate refreshOrderListVc];
      }
    } else {
      NSError *error = [OrderModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
}

#pragma mark 专家-点击待确认的“取消”
- (void)clickOnCancelBtn:(UIButton *)btn {
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  [OrderModel expertCancelOrderWithConsultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
    if (isSucceed) {
      UIWindow *window = [UIApplication sharedApplication].keyWindow;
      [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已取消预约"];
      //退到下级列表页面
      [self.navigationController popViewControllerAnimated:YES];
      //刷新订单列表数据
      if (self.delegate && [self.delegate respondsToSelector:@selector(refreshOrderListVc)]) {
        [self.delegate refreshOrderListVc];
      }
    } else {
      NSError *error = [OrderModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
    hud.hidden = YES;
  }];
}

#pragma mark 专家-点击待见面的“完成见面”
- (void)clickOnMetBtn:(UIButton *)btn {
  // 两种情况：完成见面or用户失约
  MBProgressHUD *hud = [[CommonMethod sharedCommonMethod] createProgressHUDInView:self.view withMessage:@""];
  
  if (self.noAppearBtn.isSelected) {
    //用户失约
    [OrderModel userMissedMeetWithConsultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
      if (isSucceed) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已确认用户失约"];
        //退到下级列表页面
        [self.navigationController popViewControllerAnimated:YES];
        //刷新订单列表数据
        if (self.delegate && [self.delegate respondsToSelector:@selector(refreshOrderListVc)]) {
          [self.delegate refreshOrderListVc];
        }
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
    }];
    
  }else{
    //完成见面
    [OrderModel hasBeenMeetWithConsultId:self.orderModel.orderid completion:^(BOOL isSucceed) {
      if (isSucceed) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [[CommonMethod sharedCommonMethod]showToastInCenter:window withMessage:@"已确认见面"];
        //退到下级列表页面
        [self.navigationController popViewControllerAnimated:YES];
        //刷新订单列表数据
        if (self.delegate && [self.delegate respondsToSelector:@selector(refreshOrderListVc)]) {
          [self.delegate refreshOrderListVc];
        }
      } else {
        NSError *error = [OrderModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
      hud.hidden = YES;
    }];
    
  }
  
}

#pragma mark 专家-点击“用户没出现”
- (void)clickOnAppear:(UIButton *)btn {
  btn.selected = !btn.isSelected;
}

@end
